// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {


        startButton: {
            type: cc.Node,
            default: null
        },
        loadingBar: {
            type: cc.ProgressBar,
            default: null
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        //this.startButton.on(cc.Node.EventType.MOUSE_DOWN, this.clickFunction, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.clickFunction, this);
    },

    onDestroy() {
        //this.startButton.off(cc.Node.EventType.MOUSE_DOWN, this.clickFunction, this);
        this.node.getComponent(cc.AudioSource).stop();
        this.node.off(cc.Node.EventType.TOUCH_START, this.clickFunction, this);
    },
    clickFunction(event) {
        this.startButton.active = false;

        this.loadingBar.node.active = true;
        this.loadingBar.progress = 0;

        let backup = cc.loader.onProgress;
        cc.loader.onProgress = function(count, amount) {
            this.loadingBar.progress = count / amount;
        }.bind(this);

        cc.director.preloadScene("Game", function() {
            cc.loader.onProgress = backup;
            this.loadingBar.node.active = false;
            this.startButton.active = true;
            cc.director.loadScene("Game");
        }.bind(this));
    },
    start() {

    },

    // update (dt) {},
});